package com.jk.util;

import com.alibaba.dubbo.common.utils.StringUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
/**
 * 获取客户端IP地址 工具类
 *
 * @author 李卓
 * @create 2017-12-22 10:06
 **/
public class GetIP {
    public static String getIp(HttpServletRequest request) {
                 String remoteAddr = request.getRemoteAddr();
                String forwarded = request.getHeader("X-Forwarded-For");
               String realIp = request.getHeader("X-Real-IP");
               String ip = null;
                if (realIp == null) {
                        if (forwarded == null) {
                ip = remoteAddr;
            } else {
                                ip = remoteAddr + "/" + forwarded;
                            }
                    } else {
                        if (realIp.equals(forwarded)) {
                                ip = realIp;
            } else {
                               ip = realIp + "/" + forwarded.replaceAll(", " + realIp, "");
                           }
                    }
                return ip;
             }
}
